home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Online / hsc / source / hscprj / document.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-02  |  8.6 KB  |  441 lines

  1. /*
  2.  * This source code is part of hsc, a html-preprocessor,
  3.  * Copyright (C) 1995-1997  Thomas Aglassinger
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20. /*
  21.  * hsclib/document.c
  22.  *
  23.  * document routines for hsc
  24.  *
  25.  * updated: 20-Feb-1997
  26.  * created:  7-Jul-1996
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. #include "hscprj/document.h"
  33.  
  34. /* debuggin define */
  35. #ifdef D
  36. #undef D
  37. #endif
  38. #if defined DEBUG
  39. #if DEBUG
  40. #define D(x) x
  41. #else
  42. #define D(x)                    /* nufin */
  43. #endif
  44. #else
  45. #define D(x)                    /* nufin */
  46. #endif
  47.  
  48. /*
  49.  * del/new_document
  50.  */
  51.  
  52. /* del_document: remove document */
  53. VOID del_document(APTR data)
  54. {
  55.     if (data)
  56.     {
  57.         HSCDOC *document = (HSCDOC *) data;
  58.  
  59.         ufreestr(document->docname);
  60.         ufreestr(document->sourcename);
  61.         del_estr(document->title);
  62.         del_dllist(document->iddefs);
  63.         del_dllist(document->includes);
  64.         del_dllist(document->references);
  65.         ufree(document);
  66.     }
  67. }
  68.  
  69. /* new_document: create new document */
  70. HSCDOC *new_document(STRPTR docname)
  71. {
  72.     HSCDOC *newdoc = (HSCDOC *) umalloc(sizeof(HSCDOC));
  73.  
  74. #if 0
  75.     DP(fprintf(stderr, DHP "new document `%s'\n", docname));
  76. #endif
  77.     newdoc->docname = strclone(docname);
  78.     newdoc->sourcename = NULL;
  79.     newdoc->title = init_estr(0);
  80.     newdoc->iddefs = init_dllist(del_iddef);
  81.     newdoc->includes = init_dllist(del_include);
  82.     newdoc->references = init_dllist(del_reference);
  83.  
  84.     return (newdoc);
  85. }
  86.  
  87. /*
  88.  * cmp_document: compare document
  89.  */
  90. int cmp_document(APTR cmp_data, APTR list_data)
  91. {
  92.     HSCDOC *document = (HSCDOC *) list_data;
  93.     STRPTR fname1 = NULL;
  94.     STRPTR fname2 = (STRPTR) cmp_data;
  95.  
  96. #if DEBUG
  97.     if (!document)
  98.     {
  99.         panic("documant = NULL");
  100.     }
  101.     else if (!document->docname)
  102.     {
  103.         panic("fname1 = NULL");
  104.     }
  105.     if (!fname2)
  106.     {
  107.         panic("fname2 = NULL");
  108.     }
  109. #endif
  110.     fname1 = document->docname;
  111.  
  112.     if (!strcmp(fname1, fname2))
  113.     {
  114.         return (-1);
  115.     }
  116.     else
  117.     {
  118.         return (0);
  119.     }
  120. }
  121.  
  122. /* find_document_node: scans document list for a specific document */
  123. DLNODE *find_document_node(DLLIST * list, STRPTR name)
  124. {
  125.     return (find_dlnode(dll_first(list), (APTR) name, cmp_document));
  126. }
  127.  
  128. /* find_document: scans document list for a specific document */
  129. HSCDOC *find_document(DLLIST * list, STRPTR name)
  130. {
  131.     DLNODE *nd = find_document_node(list, name);
  132.     HSCDOC *document = NULL;
  133.  
  134.     if (nd)
  135.         document = (HSCDOC *) dln_data(nd);
  136.  
  137.     return (document);
  138. }
  139.  
  140. /*
  141.  * functions for caller
  142.  */
  143. CALLER *new_caller(STRPTR name, ULONG posx, ULONG posy)
  144. {
  145.     CALLER *caller = (CALLER *) umalloc(sizeof(CALLER));
  146.  
  147.     caller->name = strclone(name);
  148.     caller->posx = posx;
  149.     caller->posy = posy;
  150.  
  151.     return (caller);
  152. }
  153.  
  154. VOID del_caller(APTR data)
  155. {
  156.     if (data)
  157.     {
  158.         CALLER *caller = (CALLER *) data;
  159.         ufreestr(caller->name);
  160.         ufree(caller);
  161.     }
  162. }
  163.  
  164. CALLER *fpos2caller(INFILEPOS * fpos)
  165. {
  166.     CALLER *cal = NULL;
  167.  
  168.     if (fpos)
  169.     {
  170.         cal = new_caller(ifp_get_fname(fpos),
  171.                          ifp_get_x(fpos), ifp_get_y(fpos));
  172.     }
  173.     else
  174.         cal = new_caller("", 0, 0);
  175.  
  176.     return (cal);
  177. }
  178.  
  179. /*
  180.  * functions for references
  181.  */
  182.  
  183. /* del_reference: remove a reference */
  184. VOID del_reference(APTR data)
  185. {
  186.     HSCREF *ref = (HSCREF *) data;
  187.  
  188.     ufreestr(ref->name);
  189.     del_caller(ref->caller);
  190.     ufree(ref);
  191.     /* NOTE: ref->fpos is removed whith the
  192.      * corresponding file automatically */
  193. }
  194.  
  195. /* new_reference: create a new reference */
  196. HSCREF *new_reference(STRPTR newname)
  197. {
  198.     HSCREF *ref = (HSCREF *) umalloc(sizeof(HSCREF));
  199.  
  200.     if (ref)
  201.     {
  202.         ref->name = strclone(newname);
  203.         ref->caller = NULL;
  204.     }
  205.     return (ref);
  206. }
  207.  
  208. /*
  209.  * cmp_reference: compare reference
  210.  */
  211. int cmp_reference(APTR cmp_data, APTR list_data)
  212. {
  213.     HSCREF *reference = (HSCREF *) list_data;
  214.     STRPTR fname1 = NULL;
  215.     STRPTR fname2 = (STRPTR) cmp_data;
  216.  
  217. #if DEBUG
  218.     if (!reference)
  219.     {
  220.         panic("reference = NULL");
  221.     }
  222.     else if (!reference->name)
  223.     {
  224.         panic("fname1 = NULL");
  225.     }
  226.     if (!fname2)
  227.     {
  228.         panic("fname2 = NULL");
  229.     }
  230. #endif
  231.     fname1 = reference->name;
  232.  
  233.     if (!strcmp(fname1, fname2))
  234.     {
  235.         return (-1);
  236.     }
  237.     else
  238.     {
  239.         return (0);
  240.     }
  241. }
  242.  
  243. /*
  244.  * app_reference: append new reference to reflist of a document;
  245.  *   if reference already exists, no new ref is added
  246.  */
  247. HSCREF *app_reference(HSCDOC * document, STRPTR ref_name)
  248. {
  249.     HSCREF *ref = NULL;
  250.  
  251.     /* append new reference */
  252.     ref = new_reference(ref_name);
  253.     app_dlnode(document->references, (APTR) ref);
  254. #if 0
  255.     DP(fprintf(stderr, DHP "new reference `%s'\n", ref_name));
  256. #endif
  257.  
  258.     return (ref);
  259. }
  260.  
  261. /*
  262.  * functions for includes
  263.  */
  264.  
  265. /* del_include: remove a include */
  266. VOID del_include(APTR data)
  267. {
  268.     HSCINC *inc = (HSCINC *) data;
  269.  
  270.     ufreestr(inc->name);
  271.     del_caller(inc->caller);
  272.     ufree(inc);
  273. }
  274.  
  275. /* new_include: create a new include */
  276. HSCINC *new_include(STRPTR newname)
  277. {
  278.     HSCINC *inc = (HSCINC *) umalloc(sizeof(HSCINC));
  279.  
  280.     if (inc)
  281.     {
  282.         inc->name = strclone(newname);
  283.         inc->caller = NULL;
  284.     }
  285.     return (inc);
  286. }
  287.  
  288. /*
  289.  * cmp_include: compare include
  290.  */
  291. int cmp_include(APTR cmp_data, APTR list_data)
  292. {
  293.     HSCINC *include = (HSCINC *) list_data;
  294.     STRPTR fname1 = NULL;
  295.     STRPTR fname2 = (STRPTR) cmp_data;
  296.  
  297. #if DEBUG
  298.     if (!include)
  299.     {
  300.         panic("include = NULL");
  301.     }
  302.     else if (!include->name)
  303.     {
  304.         panic("fname1 = NULL");
  305.     }
  306.     if (!fname2)
  307.     {
  308.         panic("fname2 = NULL");
  309.     }
  310. #endif
  311.     fname1 = include->name;
  312.  
  313.     if (!strcmp(fname1, fname2))
  314.         return (-1);
  315.     else
  316.         return (0);
  317. }
  318.  
  319. /*
  320.  * app_include: append new include to inclist of a document;
  321.  *   even append multiple instancies of the same include
  322.  */
  323. HSCINC *app_include(HSCDOC * document, STRPTR inc_name)
  324. {
  325.     DLNODE *nd = NULL;
  326.     /* append new include */
  327.     HSCINC *inc = new_include(inc_name);
  328.     nd = app_dlnode(document->includes, (APTR) inc);
  329.  
  330.     return (inc);
  331. }
  332.  
  333. /*
  334.  * functions for id-definitions
  335.  */
  336.  
  337. /* del_iddef: remove an id-definition */
  338. VOID del_iddef(APTR data)
  339. {
  340.     HSCIDD *iddef = (HSCIDD *) data;
  341.  
  342.     ufreestr(iddef->name);
  343.     del_caller(iddef->caller);
  344.     if (iddef->fpos)
  345.         del_infilepos(iddef->fpos);
  346.     ufree(iddef);
  347. }
  348.  
  349. /* new_iddef: create a new id-definition */
  350. HSCIDD *new_iddef(STRPTR newname)
  351. {
  352.     HSCIDD *iddef = (HSCIDD *) umalloc(sizeof(HSCIDD));
  353.  
  354.     if (iddef)
  355.     {
  356.         iddef->name = strclone(newname);
  357.         iddef->caller = NULL;
  358.         iddef->fpos = NULL;
  359.     }
  360.     return (iddef);
  361. }
  362.  
  363. /*
  364.  * debugging printf functions
  365.  */
  366. VOID prt_iddef(FILE * stream, APTR data)
  367. {
  368.     HSCIDD *iddef = (HSCIDD *) data;
  369.     INFILEPOS *fpos = iddef->fpos;
  370.  
  371.     fprintf(stream, "`%s' at (%lu,%lu)\n",
  372.             iddef->name, ifp_get_y(fpos), ifp_get_x(fpos));
  373. }
  374.  
  375. /*
  376.  * cmp_iddef: compare id-definition
  377.  */
  378. int cmp_iddef(APTR cmp_data, APTR list_data)
  379. {
  380.     HSCIDD *iddef = (HSCIDD *) list_data;
  381.     STRPTR fname1 = NULL;
  382.     STRPTR fname2 = (STRPTR) cmp_data;
  383.  
  384. #if DEBUG
  385.     if (!iddef)
  386.     {
  387.         panic("id-definition = NULL");
  388.     }
  389.     else if (!iddef->name)
  390.     {
  391.         panic("fname1 = NULL");
  392.     }
  393.     if (!fname2)
  394.     {
  395.         panic("fname2 = NULL");
  396.     }
  397. #endif
  398.     fname1 = iddef->name;
  399.  
  400.     if (!strcmp(fname1, fname2))
  401.     {
  402.         return (-1);
  403.     }
  404.     else
  405.     {
  406.         return (0);
  407.     }
  408. }
  409.  
  410. /* find_iddef: scans document list for a specific id */
  411. HSCIDD *find_iddef(HSCDOC * document, STRPTR name)
  412. {
  413.     DLNODE *nd = find_dlnode(dll_first(document->iddefs),
  414.                              (APTR) name, cmp_iddef);
  415.     HSCIDD *iddef = NULL;
  416.  
  417.     if (nd)
  418.     {
  419.         iddef = (HSCIDD *) dln_data(nd);
  420.     }
  421.  
  422.     return (iddef);
  423. }
  424.  
  425. /*
  426.  * app_iddef: append new id-definition to iddef-list of a document
  427.  */
  428. HSCIDD *app_iddef(HSCDOC * document, STRPTR iddef_name)
  429. {
  430.     HSCIDD *iddef = NULL;
  431.  
  432.     /* append new id-definition */
  433.     iddef = new_iddef(iddef_name);
  434.     app_dlnode(document->iddefs, (APTR) iddef);
  435. #if 0
  436.     DP(fprintf(stderr, DHP "new id-definition `%s'\n", iddef_name));
  437. #endif
  438.  
  439.     return (iddef);
  440. }
  441.